package com.rain.cy.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.rain.cy.business.mapper.UserScoreMapper;
import com.rain.cy.business.model.dto.CountDataDTO;
import com.rain.cy.business.model.dto.PatientHomeDataDTO;
import com.rain.cy.business.model.entity.UserScoreEntity;
import com.rain.cy.framework.entity.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 患者分数记录 服务实现类
 * </p>
 *
 * @author yuchen
 * @since 2021-10-07
 */
@Service
public class UserScoreService extends BaseServiceImpl<UserScoreMapper, UserScoreEntity> {

    @Autowired
    private UserProcessDataService userProcessDataService;

    /**
     * 所有分数加一起取平均值
     */
    public PatientHomeDataDTO getUserProcessScoreByPatientId(Integer userId) {
        CountDataDTO countDataFourDTO = userProcessDataService.buildFourCountData(userId);
        CountDataDTO countDataFiveDTO = userProcessDataService.buildFiveCountData(userId);
        PatientHomeDataDTO processScoreDTO = PatientHomeDataDTO.of(countDataFourDTO.getScoreAvg(), countDataFiveDTO.getScoreAvg());
        UserScoreEntity scoreEntity = lambdaQuery().eq(UserScoreEntity::getUserId, userId).eq(UserScoreEntity::getCreateDay, DateUtil.today()).last("limit 1").one();
        if (scoreEntity == null) {
            scoreEntity = new UserScoreEntity();
        }
        BeanUtil.copyProperties(processScoreDTO, scoreEntity);
        scoreEntity.setUserId(userId);
        scoreEntity.setCreateDay(DateUtil.today());
        saveOrUpdate(scoreEntity);
        return processScoreDTO;
    }
}
